﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;
using UltimatePvPSuite;
using Styx.CommonBot;
using Styx.Common.Helpers;

namespace UltimatePvPSuite
{
    public static class Interrupts
    {
        private static readonly WaitTimer InterruptTimer = new WaitTimer(TimeSpan.FromMilliseconds(3000));

        public static bool Pulse()
        {
            if (UltimatePvPSuite.EnableInterrupts && InterruptTimer.IsFinished)
            {
                if (UseFirstInterrupt())
                {
                    InterruptTimer.Reset();
                    return true;
                }
                else if (UseSecondInterrupt())
                {
                    InterruptTimer.Reset();
                    return true;
                }
                else if (UseThirdnterrupt())
                {
                    InterruptTimer.Reset();
                    return true;
                }
            }
            return false;
        }
        private static WoWPlayer InterruptCheck(int range)
        {
            using (StyxWoW.Memory.AcquireFrame())
            {
                return (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(false)
                        orderby Unit.Distance
                        where Unit.IsAlive
                        where Unit.IsPlayer
                        where Unit.Distance < range
                        where !Unit.IsFriendly
                        where !Unit.IsPet
                        where Unit.InLineOfSight
                        where Unit.IsCasting
                        where !Unit.HasAura("Spirit of Redemption")
                        where !Unit.HasAura("Blessing of Protection")
                        where !Unit.HasAura("Divine Shield")
                        where UltimatePvPSuite.InterruptSpells.Contains(Unit.CastingSpell.Name)
                        select Unit).FirstOrDefault();
            }
        }

        private static bool UseFirstInterrupt()
        {
            if (UltimatePvPSuite.EnableFirstInterrupt && UltimatePvPSuite.FirstInterruptSpell != "")
            {
                int range = Convert.ToInt32(UltimatePvPSuite.SecondSpellRange);
                WoWPlayer target = InterruptCheck(range);

                //Logger.Write(target.Name + " " + range + " " + UltimatePvPSuite.FirstInterruptSpell);
                if (SpellManager.CanCast(UltimatePvPSuite.FirstInterruptSpell) && target != null)
                {
                    Logger.Write(target.SafeName() + " is casting " +target.CastingSpell.Name + ". Using " + UltimatePvPSuite.FirstInterruptSpell + " to interrupt.");
                    SpellManager.Cast(UltimatePvPSuite.FirstInterruptSpell, target);
                    return true;
                }
            }
            return false;
        }
        private static bool UseSecondInterrupt()
        {
            if (UltimatePvPSuite.EnableSecondInterrupt && UltimatePvPSuite.SecondInterruptSpell != "")
            {
                int range = Convert.ToInt32(UltimatePvPSuite.SecondSpellRange);
                WoWPlayer target = InterruptCheck(range);

                //Logger.Write(target.Name + " " + range + " " + UltimatePvPSuite.SecondInterruptSpell);
                if (SpellManager.CanCast(UltimatePvPSuite.SecondInterruptSpell) && target != null)
                {
                    Logger.Write(target.SafeName() + " is casting " + target.CastingSpell.Name + ". Using " + UltimatePvPSuite.SecondInterruptSpell + " to interrupt.");
                    SpellManager.Cast(UltimatePvPSuite.SecondInterruptSpell, target);
                    return true;
                }
            }
            return false;
        }
        private static bool UseThirdnterrupt()
        {
            if (UltimatePvPSuite.EnableThirdInterrupt && UltimatePvPSuite.ThirdInterruptSpell != "")
            {
                int range = Convert.ToInt32(UltimatePvPSuite.SecondSpellRange);
                WoWPlayer target = InterruptCheck(range);

                //Logger.Write(target.Name + " " + range + " " + UltimatePvPSuite.ThirdInterruptSpell);
                if (SpellManager.CanCast(UltimatePvPSuite.ThirdInterruptSpell) && target != null)
                {
                    Logger.Write(target.SafeName() + " is casting " + target.CastingSpell.Name + ". Using " + UltimatePvPSuite.ThirdInterruptSpell + " to interrupt.");
                    SpellManager.Cast(UltimatePvPSuite.ThirdInterruptSpell, target);
                    return true;
                }
            }
            return false;
        }
    }
}
